package com.atguigu.springmvc.demo01.controller;

import com.atguigu.springmvc.demo01.bean.User;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yanglin
 * @create 2023-12-21 16:06 星期四
 * description:
 */
@Controller
public class HttpController {

    /**
     * 测试 @RequestBody
     * @param requestBody
     * @return
     */
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String requestBody){
        System.out.println(requestBody);
        return "success";
    }
    
    @RequestMapping("/testRequestEntity")
    public String testRequestEntity(RequestEntity<String> requestEntity){
        //当前requestEntity表示整个请求报文的信息
        System.out.println("请求头：" + requestEntity.getHeaders());
        System.out.println("请求体：" + requestEntity.getBody());
        return "success";
    }
    
    @RequestMapping("/testResponse")
    public void testResponse(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello response");
    }
    
    
    @RequestMapping("/testResponseBody")
    @ResponseBody
    public String testResponseBody(){
        return "success";
    }
    
    @RequestMapping("/testResponseUser")
    @ResponseBody
    public User testResponseUser(){
        User user = new User();
        user.setId(1001);
        user.setUsername("admin");
        user.setPassword("admin");
        user.setAge(18);
        user.setSex("男");
        user.setEmail("haha@qq.com");
        return user;
    }
    
    @PostMapping("/testAxios")
    @ResponseBody
    public String testAxios(String username,String password){
        System.out.println(username + "  " + password);
        return "hello axios";
    }
    
}
